$('#right-float').on('outview.scrollspy.amui', function() {
    console.log('离开视口');
});
var app = new Vue({
    el: "#app",
    data: {
        Inimg: {
            img1: './img/insert.png',
            img1base64: '',
            img2: './img/insert.png',
            img2base64: '',
            img3: './img/insert.png',
            img3base64: '',
            defaultimg: './img/insert.png',
        },
        Send: {
            Text: '',
            Style: 'width:90%;',
            Class: 'progress-bar-warning',
        },
        TweetText: '',
        UserInfo: {
            Userid: null,
            LoginName: "none",
            UserName: "none",
            UserHead: './img/defaulthead.png',
            Simple: '这个人很懒，没有个人简介',
            BornDate: '0000-00-00',
            Sex: '男',
        },
        Tweet: [],
        BigImg: './img/defaulthead.png',
        AlertText: '默认内容',
        Reply:[],
        editorList:[],
        headList:[
            {
                img:'http://s.amazeui.org/media/i/demos/bing-1.jpg',
                text:'远方 有一个地方 那里种有我们的梦想'
            },
            {
                img:'http://s.amazeui.org/media/i/demos/bing-2.jpg',
                text:'远方 有一个地方 那里种有我们的梦想'
            },
            {
                img:'http://s.amazeui.org/media/i/demos/bing-3.jpg',
                text:'远方 有一个地方 那里种有我们的梦想'
            },
            {
                img:'http://s.amazeui.org/media/i/demos/bing-4.jpg',
                text:'远方 有一个地方 那里种有我们的梦想'
            }
        ],
    },
    methods: {
        ReadImage: function (e, value) {
            var Point = this;
            var image = e.target.files[0];
            if (image.size > 2101440) {
                alert("图片过大，请选择小于2M的图片");
                return false;
            }
            var reader = new FileReader();
            reader.readAsDataURL(image);
            reader.onload = function (e) {
                if (value == 1) {
                    Point.Inimg.img1 = e.target.result;
                    Point.Inimg.img1base64 = Point.Inimg.img1.substring(Point.Inimg.img1.indexOf(",") + 1);
                } else if (value == 2) {
                    Point.Inimg.img2 = e.target.result;
                    Point.Inimg.img2base64 = Point.Inimg.img2.substring(Point.Inimg.img2.indexOf(",") + 1);
                } else if (value == 3) {
                    Point.Inimg.img3 = e.target.result;
                    Point.Inimg.img3base64 = Point.Inimg.img3.substring(Point.Inimg.img3.indexOf(",") + 1);
                }
            }
        },
        SendTweet: function () {
// 					this.Send.Class="progress-bar-warning";
// 					this.Send.Style="width:50%";
            $.AMUI.progress.start();
            if (this.TweetText.length == 0) {
// 						this.Send.Text="未输入内容，上传失败";
// 						this.Send.Style="width:100%";
// 						this.Send.Class="progress-bar-danger";
                this.AlertText = "请输入内容";
                $("#my-alert").modal('toggle');
                $.AMUI.progress.done();
                return false;
            }
            $.post("./createtweet", {
                text: this.TweetText,
                img1: this.Inimg.img1base64,
                img2: this.Inimg.img2base64,
                img3: this.Inimg.img3base64,
            }, function (response) {
                if (response == "success") {
                    $.AMUI.progress.done();
                    app.AlertText = "发布完成，刷新后可见！";
                    $("#my-alert").modal('toggle');
// 							app.Send.Text="发送成功！";
// 							app.Send.Style="width:100%";
// 							app.Send.Class="progress-bar-success";
                } else {
                    $.AMUI.progress.done();
                    app.AlertText = "发布失败，内容无效！";
                    $("#my-alert").modal('toggle');
// 							app.Send.Text="发送失败！";
// 							app.Send.Style="width:100%";
// 							app.Send.Class="progress-bar-danger";
                }
                $("#modal-sendtweet").modal('close');
            });
        },
        BigImgClick: function (data) {
            // 点击查看大图
            this.BigImg = data;
        },
        ContinueLoadTweet: function () {
            //继续加载推文
            var len = this.Tweet.length - 1;
            $.post("./queryalltweet", {
                type: 'continue',
                tweet_id: this.Tweet[len].id,
            }, function (response) {
                response = JSON.parse(response);
                if (response.tweet[0] != undefined) {
                	for (var i=0;i<response.tweet.length;i++){
                		app.Tweet.push(response.tweet[i]);
                	}
                    
                } else {
                    app.AlertText = "已经全部加载完毕了";
                    $("#my-alert").modal('toggle');
                }
            });
        },
        CreatePraise: function (id, key) {
            //点赞
            var _id = id;
            $.post('./clickpraise', {
                type: 'addpraise',
                tweetid: id
            }, function (response) {
                if (response == "success") {
                    app.Tweet[key]['up']++;
                    swal({
                        text: '点赞成功',
                        type: 'success'
                    })
                } else {
                    swal({
                        text: '点赞失败',
                        type: 'error'
                    })
                }
            });
        },
        CreateReply: function (id, key) {
            //添加评论

            swal({
                title: '添加评论',
                input: 'text',
                showCancelButton: true,
                inputValidator: function (value) {
                    if (value.replace(' ','')!='' && value!=''){
                        $.post('./clickpraise', {
                            type: 'addreply',
                            tweetid: id,
                            text: value,
                        }, function (response) {
                            if (response == "success") {
                                swal({
                                    type: 'success',
                                    text: '评论成功'
                                });
                            }else{
                                swal({
                                    type: 'error',
                                    text: '评论失败'
                                });
                            }

                        })
                    }
                    return new Promise(function (resolve, reject) {
                        if (value) {
                            resolve();
                        } else {
                            reject('你需要写入内容');
                        }
                    });
                }
            });

        },
        Loadreply:function (id,key) {
            //加载评论
            // queryreply
            var _this=this;
            var start=0;
            if (this.Tweet[key].Reply!=undefined){
                start=this.Tweet[key].Reply.length
            }
            var len=2;
            $.post('./clickpraise',{
                type:'queryreply',
                tweetid: id,
                start:start,
                len:len
            },function (result) {
                result = JSON.parse(result);
                if (_this.Tweet[key].Reply==undefined){
                	//第一次需要手动 更新视图
                    if (result.reply.length==0){
                        app.$set(_this.Tweet[key],'Reply',[{username:'',createdate:'',text:'无评论'}]);
                    }else{
                        app.$set(_this.Tweet[key],'Reply',result.reply);
                    }

                }else if(result.reply.length!=0){
                	_this.Tweet[key].Reply.push(result.reply);
                }else{
                	swal({
                        type: 'error',
                        text: '已经加载全部了，不要请求了！！！！'
                    });
                }
                
//                _this.Tweet[key].Reply(result.reply);
            })

        },
        queryEditorList: function () {
            //查询文章列表
            $.post('./queryeditor', {}, function (response) {
                console.log(response);
                if (response != "") {
                    response = JSON.parse(response);
                    Vue.set(app, "editorList", response.result);
                }
            });
        }
    },
    mounted:function(){

    },
    created: function () {
        //请求当前用户信息
        $.post("./userinfo", function (response) {
            response = JSON.parse(response);
            app.UserInfo.Userid = response.id;
            app.UserInfo.LoginName = response.login_name;
            app.UserInfo.UserName = response.user_name;
            app.UserInfo.UserHead = response.user_head;
            app.UserInfo.TweetNumber=response.tweet_number;
            app.UserInfo.PraiseNumber=response.praise_number;
            if (response.simple != undefined) {
                app.UserInfo.Simple = response.simple;
            }
            if (response.borndate != undefined) {
                app.UserInfo.BornDate = response.borndate;
            }
            if (response.sex != undefined) {
                app.UserInfo.Sex = response.sex;
            }
        });
        //请求最新的推文
        $.post("./queryalltweet", {
            type: 'new'
        }, function (response) {
            if (response!=""){
                // console.log(response);
                response = JSON.parse(response);
                // app.Tweet = response.tweet;
                Vue.set(app,'Tweet',response.tweet);
            }

        });
        //请求轮播图
        $.post("./queryhead",{},function (result) {
            if (result!=""){
                result=JSON.parse(result);
                // Vue.set(app,'headList',result.result);
                for (var i=0;i<result.result.length;i++){
                    Vue.set(app.headList[i],'text',result.result[i].text);
                    Vue.set(app.headList[i],'img',"./image?src="+result.result[i].img);
                }
            }
        });
        // console.log($("#right-float"));
        // 请求公告
        this.queryEditorList();

    }
});